package com.shenmazong.servereduschool.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenmazong.servereduschool.controller.TbStaffController;
import com.shenmazong.servereduschool.pojo.TbStaff;
import com.shenmazong.servereduschool.service.TbStaffService;
import com.shenmazong.servereduschool.mapper.TbStaffMapper;
import com.shenmazong.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbStaffServiceImpl extends ServiceImpl<TbStaffMapper, TbStaff>
implements TbStaffService{
    @Autowired
    TbStaffMapper tbStaffMapper;

    @Override
    public PageInfo findStaffList(Integer pageNum, Integer pageSize, TbStaff tbStaff) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbStaff> tbStaffs=tbStaffMapper.findStaffList(tbStaff);
        PageInfo<TbStaff> pageInfo = new PageInfo<>(tbStaffs);
        return pageInfo;
    }

    @Override
    public ResultResponse add(TbStaff tbStaff) {
        tbStaff.setCreateTime(new Date());
        tbStaff.setUpdateTime(new Date());
        int i = tbStaffMapper.insert(tbStaff);
        if(i<1){
            return ResultResponse.FAILED(-1,"添加失败");
        }
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse queryByStaffId(Long staffId) {
        TbStaff tbStaff = tbStaffMapper.selectById(staffId);
        if(tbStaff==null){
            return ResultResponse.FAILED(-1,"回显失败");
        }
        return ResultResponse.SUCCESS(tbStaff);
    }

    @Override
    public ResultResponse updateStaff(TbStaff tbStaff) {
        int i = tbStaffMapper.updateById(tbStaff);
        if(i<0){
            return ResultResponse.FAILED(-1,"修改失败");
        }
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse delByStaffId(Long staffId) {
        int i = tbStaffMapper.deleteById(staffId);
        if(i<0){
            return ResultResponse.FAILED(-1,"删除失败");
        }
        return ResultResponse.SUCCESS("删除成功");
    }

    @Override
    public ResultResponse delAll(List<TbStaff> ids) {
        if(ids.size()<=0){
            return  ResultResponse.FAILED(-1,"至少选择一个");
        }
        for (TbStaff staff : ids) {
            tbStaffMapper.deleteById(staff.getStaffId());
        }
        return ResultResponse.SUCCESS("删除成功");
    }
}




